﻿package com.element.role 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author zhengzihua
	 */
	public class RoleBehavior implements IRole
	{
		
		public function RoleBehavior() 
		{
			
		}
		public function jugmentDirection(p1:Point, p2:Point):String {
			var rotation:int = Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI;
			if (rotation < 22&&rotation>=-23) {
				return "right";
			}else if (rotation >= 22 && rotation < 67) {
				return "rightDown";
			}else if (rotation >= 67 && rotation < 112) {
				return "down";
			}else if (rotation >= 112 && rotation < 157) {
				return "leftDown";
			}else if (rotation >= 157) {
				return "left"
			}else if (rotation>=-68&&rotation<-23) {
				return "rightUp"
			}else if (rotation >= -113 && rotation < -68) {
				return "up";
			}else if (rotation >= -158 && rotation < -113) {
				return "leftUp";
			}else if (rotation < -158) {
				return "left";
			}
			return null;
		}
		public function walk(str:String, speed:int, role:MovieClip,instance:Sprite):void {
			var manSpeed:int = speed;
			var man:MovieClip = role;
			switch(str) {
				case "down":
					instance.y += manSpeed;
					if (man.currentFrame >= 2 && man.currentFrame <= 7) {
						//已经是这个运动方向
					}else {
						man.gotoAndStop(str);
					}
					break;
				case "leftDown":
					instance.x -= manSpeed;
					instance.y += manSpeed * 0.5;
					if (man.currentFrame >= 9 && man.currentFrame <= 14) {
						//已经是这个运动方向
					}else {
						man.gotoAndStop(str);
					}
					break;
				case "left":
					instance.x -= manSpeed;
					if (man.currentFrame >= 16 && man.currentFrame <= 21) {
						//已经是这个运动方向
					}else {
						man.gotoAndStop(str);
					}
					break;
				case "leftUp":
					instance.x -= manSpeed;
					instance.y -= manSpeed * 0.5;
					if (man.currentFrame >= 23 && man.currentFrame <= 28) {
						//已经是这个运动方向
					}else {
						man.gotoAndStop(str);
					}
					break;
				case "up":
					instance.y -= manSpeed;
					if (man.currentFrame >= 30 && man.currentFrame <= 35) {
						//已经是这个运动方向
					}else {
						man.gotoAndStop(str);
					}
					break;
				case "rightUp":
					instance.x += manSpeed;
					instance.y -= manSpeed * 0.5;
					if (man.currentFrame >= 37 && man.currentFrame <= 42) {
						//已经是这个运动方向
					}else {
						man.gotoAndStop(str);
					}
					break;
				case "right":
					instance.x += manSpeed;
					if (man.currentFrame >= 44 && man.currentFrame <= 49) {
						//已经是这个运动方向
					}else {
						man.gotoAndStop(str);
					}
					break;
				case "rightDown":
					instance.x += manSpeed;
					instance.y += manSpeed * 0.5;
					if (man.currentFrame >= 51 && man.currentFrame <= 56) {
						//已经是这个运动方向
					}else {
						man.gotoAndStop(str);
					}
					break;
			}
			man.nextFrame()
		}
		
	}
	
}